package q646_findLongestChain;

import java.util.Arrays;
import java.util.Comparator;

public class Solution_2 {
    /*
    dp的方式则需要双重循环
    定义 dp[i] 为以 pairs[i] 为结尾的最长数对链的长度
     */
    public int findLongestChain(int[][] pairs) {
        int n = pairs.length;
        Arrays.sort(pairs, Comparator.comparingInt(a -> a[0]));
        int[] dp = new int[n];
        Arrays.fill(dp, 1);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < i; j++) {
                if (pairs[i][0] > pairs[j][1]) {
                    dp[i] = Math.max(dp[i], dp[j] + 1);
                }
            }
        }
        return dp[n - 1];
    }

}
